package com.jove.girl.controller;

import com.jove.girl.entity.Girl;
import com.jove.girl.repository.GirlRepository;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
public class GirlController {

    @Autowired
    private GirlRepository girlRepository;

    @ApiOperation(value = "获取列表", notes = "获取列表")
    @GetMapping(value = "/girls")
    public List<Girl> girlList() {
        return girlRepository.findAll();
    }

    @PostMapping(value = "/girls")
    public String insertGirls(@RequestParam(value = "cupSize", required = false, defaultValue = "A") String cupSize,
                              @RequestParam(value = "age", required = false, defaultValue = "18") Integer age) {

        Girl girl = new Girl();
        girl.setCupSize(cupSize);
        girl.setAge(age);

        girlRepository.save(girl);

        return "200";
    }

    @GetMapping(value = "/girls/{id}")
    public Optional<Girl> getGirlById(@PathVariable(value = "id") Integer id) {

        return girlRepository.findById(id);
    }

    @PutMapping(value = "girls/{id}")
    private void girlUpdate(@PathVariable(value = "id") Integer id,
                            @RequestParam(value = "cupSize", required = false, defaultValue = "A") String cupSize,
                            @RequestParam(value = "age", required = false, defaultValue = "18") Integer age) {

        Girl girl = new Girl();
        girl.setId(id);
        girl.setCupSize(cupSize);
        girl.setAge(age);

        girlRepository.save(girl);

    }

    @DeleteMapping(value = "/girls/{id}")
    public String deleteGirlById(@PathVariable(value = "id") Integer id) {

        girlRepository.deleteById(id);
        return "200";
    }

}
